package com.studio77.guessLyric.layer;

import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.util.Log;

import com.studio77.guessLyric.MainActivity;
import com.studio77.guessLyric.R;
import com.studio77.guessLyric.core.GameSystem;
import com.studio77.guessLyric.core.StageManager;
import com.studio77.guessLyric.scene.GameScene;
import com.studio77.guessLyric.util.GameUtil;
import com.wiyun.engine.actions.Action;
import com.wiyun.engine.actions.IntervalAction;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.actions.RotateTo;
import com.wiyun.engine.actions.Sequence;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.sound.AudioManager;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.types.WYPoint;

/**
 * 主菜单层
 * 
 * @author Administrator
 * 
 */
public class MainLayer extends Layer {

	Sprite background;
	Sprite title_fengkuang;
	Sprite title_caikeci;
	Sprite title_yf;
	Sprite currStageBackground;

	Button btn_newgame;
	Button btn_shop;

	Label currentStageLable;
	Label studioInfo;

	public MainLayer() {
		// 背景
		background = Sprite.make(GameSystem.tex_background);
		background.setAnchor(0, 0);
		background.setPosition(WYPoint.make(0, 0));
		// background.setScaleX(GameSystem.Screen_Width
		// / GameSystem.tex_background.getWidth());
		// background.setScaleY(GameSystem.Screen_Height
		// / GameSystem.tex_background.getHeight());
		this.addChild(background);

		// 载入title
		loadTitle();
		// 载入按钮
		loadButtons();

		// 载入当前进度
		currStageBackground = Sprite.make(GameSystem.tex_stage_star);
		currStageBackground.setPosition(240, 730);
		addChild(currStageBackground);

		currentStageLable = Label.make("", 45, Typeface.BOLD);
		currentStageLable.setColor(WYColor3B.make(255, 255, 255));
		currentStageLable.setPosition(240, 730);
		addChild(currentStageLable);

		refreshStageIndex();

		// studioInfo = Label.make("77Studio Presents 2013", 24);
		// studioInfo.setColor(WYColor3B.make(200, 200, 200));
		// studioInfo.setPosition(20, 5);
		// studioInfo.setAnchor(0, 0);
		// addChild(studioInfo);

		// Sprite introNew = Sprite.make(R.drawable.intro_newversion);
		// introNew.setPosition(240, 87);
		// addChild(introNew);

		// Button downloadNew = Button.make(R.drawable.intro_newversion,
		// R.drawable.intro_newversion, this, "onDownloadNewClicked");
		// downloadNew.setClickScale(1.1f);
		// downloadNew.setPosition(240, 87);
		// this.addChild(downloadNew);

		// 检查更新
		MainActivity.instance.UpdateVersion();
	}

	public void refreshStageIndex() {
		currentStageLable.setText((StageManager.instance.getLastStage() + 1)
				+ "");
	}

	/**
	 * 下载豪华版
	 */
	public void onDownloadNewClicked() {
		MainActivity.instance.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				Uri uri = Uri.parse("http://down.mumayi.com/272352");
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				MainActivity.instance.startActivity(intent);
			}
		});
	}

	private void loadButtons() {
		btn_newgame = Button.make(R.drawable.btn_start,
				R.drawable.btn_start_press, this, "onNewGameClicked");
		btn_newgame.setPosition(240, 280);
		btn_newgame.setAnchor(0.5f, 0.5f);
		this.addChild(btn_newgame);

		// 道具商城入口
		// btn_shop = Button.make(R.drawable.button_shop_normal,
		// R.drawable.button_shop_press, this, "onShopClicked");
		// btn_shop.setPosition(240, 252);
		// if (!AppControl.isSwitcherOn() ||
		// StageManager.instance.isShowedShop()) {
		// this.addChild(btn_shop);
		// if (!StageManager.instance.isShowedShop()) {
		// StageManager.instance.setShowedShop(true);
		// MainActivity.instance.runOnUiThread(new Runnable() {
		//
		// @Override
		// public void run() {
		// AlertDialog.Builder builder = new Builder(
		// MainActivity.instance);
		// builder.setTitle("提示");
		// builder.setMessage("去看看道具商店吧，您可以免费获取金币换取提示以及解锁关卡哦！");
		// builder.setPositiveButton("OK",
		// new DialogInterface.OnClickListener() {
		// @Override
		// public void onClick(DialogInterface dialog,
		// int which) {
		// dialog.dismiss();
		// }
		// });
		// builder.create().show();
		// }
		// });
		// }
		// }
	}

	private void loadTitle() {
		title_fengkuang = Sprite.make(GameSystem.tex_fengkuang);
		title_fengkuang.setAnchor(0.5f, 0.5f);
		title_fengkuang.setPosition(240, 600);

		title_caikeci = Sprite.make(GameSystem.tex_caigeci);
		title_caikeci.setAnchor(0.5f, 0.5f);
		title_caikeci.setPosition(240, 496);

		title_yf = Sprite.make(GameSystem.tex_yinfu);
		title_yf.setAnchor(0f, 1f);
		title_yf.setPosition(336, 600);

		addChild(title_yf);
		addChild(title_caikeci);
		addChild(title_fengkuang);
	}

	private void setRotate(Sprite target, float time, float toAngle) {
		IntervalAction rotate = (IntervalAction) RotateTo.make(time,
				toAngle * -1, toAngle).autoRelease();
		IntervalAction back = (IntervalAction) rotate.reverse().autoRelease();
		IntervalAction seq = (IntervalAction) Sequence.make(rotate, back)
				.autoRelease();
		Action repeat = (Action) RepeatForever.make(seq).autoRelease();
		target.runAction(repeat);
	}

	public void onNewGameClicked() {
		Log.d("click", "new game");
		AudioManager.playEffect(R.raw.enter);
		GameUtil.switchSceneBottomSlide(GameScene.make());
		GameScene.make().start();
		// StageScene.make().refreshStages();
		// ADHelper.hideAd();
	}

}
